/*
 * Design Assignment3.c
 *
 * Created: 2014/3/10 15:43:13
 *  Author: Rong Xue
 */ 

#include <avr/interrupt.h> //include library for interrupt purpose 
#include <avr/io.h>

int volatile TMP=0;  //volatile variable for unstable counter 

int main(void)  // main function
{
TCCR1A=1<<WGM12; // CTC mode
TCCR1B=1<<CS12;  
TIMSK1=(1<<OCIE1A);  // interrupt enable
DDRD = 0b0011; // DDRD(0,1,) as output
DDRB = 0xFF; // make DDRB as output
TCCR0A=0x00; // set Timer0 in normal mode
TCCR0B=0x06; // set clock on falling edge
OCR1A=0x3D4; // set 250ms

EICRA =(1<<ISC01); // set interrupt on ISC01
EIMSK =(1<<INT0); // enable INT0
sei(); // command for interrupts
   while(1);      
}

ISR(TIMER1_COMPA_vect)
{
    TCNT1=0;
    PORTD ^=(1<<0); // toggle PD0
}

ISR (INT0_vect) // ISR function used for interrupt purpose
{
    PORTB=TCNT0; // display counter output
	TMP++;  // increase global counter
    if(TMP ==16){ //if counter equal 2 turn on PD3 and reset counter
        PORTD = (1<<PD3);
        TMP=0;
    }
}